package artid.com.arttogoa;

import org.apache.commons.lang3.StringUtils;
import artid.com.arttogoa.app.ArtWorkApp;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.AppItem;
import artid.com.arttogoa.util.MySharedPreferences;
import artid.com.arttogoa.view.MyEditText;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称：UpdatePassActivity   
 * 类描述：修改密码   
 * 创建人：耿卫斌  
 * 创建时间：2016年5月16日 下午2:36:24      
 *
 */
public class UpdatePassActivity extends BaseActivity implements OnClickListener {
	private TextView mSubmit;
	private MyEditText oldPass, newPass;
	private CheckBox oldEye, newEye;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_pass);
		setContent(this, getIntent());
		ArtWorkApp.getInstance().addActivity(this);
		initview();
		mContext = this;
	}

	private void initview() {
		mContext = this;
		mSubmit = (TextView) findViewById(R.id.update_submit);
		mTittle = (TextView) findViewById(R.id.title_text);
		mTittle.setText("修改密码");
		setBack();
		oldPass = (MyEditText) findViewById(R.id.old_pass_edit);
		newPass = (MyEditText) findViewById(R.id.new_pass_edit);
		oldPass.addTextChangedListener(mTextChangedListener);
		newPass.addTextChangedListener(mTextChangedListener);
		oldEye = (CheckBox) findViewById(R.id.old_eye_edit);
		newEye = (CheckBox) findViewById(R.id.new_eye_edit);
		mSubmit.setOnClickListener(this);
		oldEye.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					// 如果选中，显示密码
					oldPass.setTransformationMethod(HideReturnsTransformationMethod
							.getInstance());
				} else {
					// 否则隐藏密码
					oldPass.setTransformationMethod(PasswordTransformationMethod
							.getInstance());
				}

			}
		});
		newEye.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					// 如果选中，显示密码
					newPass.setTransformationMethod(HideReturnsTransformationMethod
							.getInstance());
				} else {
					// 否则隐藏密码
					newPass.setTransformationMethod(PasswordTransformationMethod
							.getInstance());
				}

			}
		});
		conditionJudge();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_submit:
			// Intent in = new Intent(UpdatePassActivity.this,
			// LoginAvtivity.class);
			// startActivity(in);
			// finish();
			Submit();
			break;

		default:
			break;
		}

	}
	
	/**
	 * 条件判断
	 */
	private void conditionJudge(){
		if(!StringUtils.isEmpty(oldPass.getText().toString()) && !StringUtils.isEmpty(newPass.getText().toString())){
			mSubmit.setEnabled(true);
			mSubmit.setBackgroundResource(R.drawable.red_frame);
		}else{
			mSubmit.setEnabled(false);
			mSubmit.setBackgroundResource(R.drawable.zjm_bg_dl);
		}
	}
	
	/**
	 * 对EditText监听
	 */
	TextWatcher mTextChangedListener = new TextWatcher() {  
		private CharSequence temp;  
		@Override  
		public void onTextChanged(CharSequence s, int start, int before, int count) {  
			// TODO Auto-generated method stub  
			temp = s;  
		}  

		@Override  
		public void beforeTextChanged(CharSequence s, int start, int count,  
				int after) {  
			// TODO Auto-generated method stub  
			//		          mTextView.setText(s);//将输入的内容实时显示  
		}  

		@Override  
		public void afterTextChanged(Editable s) {  
			// TODO Auto-generated method stub   

			//对按钮的判断是否可用
			conditionJudge();
		}  
	};  

	/**
	 * 修改密码网络操作
	 */
	private void Submit() {
		VolleyBasic.getInstance(UpdatePassActivity.this).post(
				Cache.updatePwdData,
				VolleyMap.getInstance().modifyPwd(oldPass.getText().toString(),
						newPass.getText().toString()), AppItem.class,
						new VolleyCallback() {
					@Override
					public void onSuccess(VolleyData vd) {
						Toast.makeText(mContext, "修改成功，请重新登录", 2).show();
						MySharedPreferences.getMySharedPreferences(mContext).setValue("PassWord", "");
						Intent in = new Intent(UpdatePassActivity.this,
								LoginAvtivity.class);
						startActivity(in);
						ArtWorkApp.getInstance().exit();
					}

					@Override
					public void onError(VolleyError volleyError) {

					}
				});
	}
}
